<template>
    <el-card class="edit" shadow="never" >
        <template #header>
            <div class="edit-title">题库权限</div>
            <div class="edit-desc">用户权限</div>
        </template>
        <el-form ref="formRef" :model="form" :rules="formRules" label-width="100" v-loading="loading">
            <el-form-item label="题库用户：" prop="userIdList">
                <Select
                    v-model="form.userIdList"
                    url="user/listpage"
                    :params="{ state: 1 }"
                    search-parm-name="name"
                    option-label="name"
                    option-value="id"
                    :options="examUsers"
                    :multiple="true"
                    clearable
                    searchPlaceholder="请输入机构名称或用户名称进行筛选"
                    >
                    <template #default="{ option }">
                        {{ option.name }} - {{option.orgName}}
                    </template>
                </Select>
            </el-form-item>
        </el-form>
        <el-button class="edit-ok" type="primary" @click="next">确定</el-button>
    </el-card>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useExamStore } from '@/stores/exam';
import Select from '@/components/Select.vue'
import http from '@/request';
import { useRouter,useRoute } from 'vue-router'

const router = useRouter();
const route = useRoute()
const formRef = ref<FormInstance>()// 表单引用
const formRules = reactive<FormRules>({// 表单校验规则
    userIdList: [
        { required: true, message: '请选择考试用户', trigger: 'blur' },
    ],
})
const examUsers = ref([] as any[]) // 选中用户
const form = reactive({// 表单
    questionTypeId: 0,
    userIdList: ref([] as any[])
})
const loading = ref(true) ;

// 组件挂载完成后，执行如下方法
onMounted(async () => {
        form.questionTypeId = parseInt(route.params.id as string)
            let { data: { data } } = await http.post("userQuestion/getUserListByQuestionTypeId", { 
                questionTypeId: form.questionTypeId,
            })
            
            examUsers.value.push(...data)
            for(let key in data){
                form.userIdList.push(data[key].id);
            }
            loading.value = false;
})

// 下一步
async function next() {
    if (!formRef.value) return
    await formRef.value.validate(async (valid, fields) => {
        if (!valid) {
            return
        }

        let { data: { code } } = await http.post("userQuestion/setting", {...form})
        if (code !== 200) {
            return
        }

        router.push("/questionType")
    })
}
</script>
  
<style lang="scss" scoped>
.edit {
    flex: 1;
    overflow: visible;
    padding: 10px;

    .edit-ok{
        margin-left: 10px;
        width: 70px;
        font-size: 14px;
    }
    :deep(.el-card__header) {
        padding-bottom: 0px;

        .edit-title {
            font-size: 14px;
            font-weight: bold;
            padding-bottom: 5px;
        }

        .edit-desc {
            font-size: 12px;
            color: var(--el-text-color-secondary);
        }

    }

}
</style>